integer attachment = TRUE;
integer relaychannel = -1812221819;
string cmdname;
key victim;
list gMenu=[];
list statesMenu=[0,0,0,0];
list gAvatarAllow;
key ToucherID;
integer gMenuLink =1337;
integer gDemon =1338;
integer gPlugin =1339;
integer gVic =1329;
integer channel_dialog;
integer listen_id; //menu
integer listen_id2; //Chat
integer listen_id3; //Emote
integer listen_id4; //Name
integer listen_id5; //Name
integer listen_id6; //Name
string msg ="Hover Test";
list sideMenu=[" ","Back"," "];
list gEmote=[];
list gChat=[];
string wearer;
string objektname;

string gsCardName = "Dv_Boots"; //CardName
list glCardRows;	//rows readed
key gkQuery;		//Filereader
integer giLine = 0;	//Line
integer emoteChan=123456;
integer chatChan=123457;
integer rand = 13;
integer addEmote=15;
integer addChat=14;
Debug(string _text)
{
	llSay(DEBUG_CHANNEL, _text);
}

llRlvSay(string command)
{
	if(attachment==TRUE)
	{
		llOwnerSay("@"+command);
	}
	else
	{
		llSay(relaychannel,cmdname + "," + (string)victim  + "," + "@"+command);
	}
}

MakeRLVState()
{
	if(attachment==TRUE)
	{
		//onRez we make this to be secure if its an attachmentttt
		if(llList2Integer(statesMenu,0)==1)llRlvSay("redirchat:"+(string)chatChan+"=add");
		if(llList2Integer(statesMenu,1)==1)llRlvSay("rediremote:"+(string)emoteChan+"=add");
		if(llList2Integer(statesMenu,2)==1)llRlvSay("sendim_sec=n");
		if(llList2Integer(statesMenu,3)==1)llRlvSay("recvim_sec=n");
		if(gChat==[])
		{
			glCardRows=[];
			gsCardName="Chat";
			giLine=0;
			gkQuery = llGetNotecardLine(gsCardName,giLine);
		}
	}
}

llLRP(integer element_new, integer placeinlist)
{
	statesMenu = llListReplaceList(statesMenu, [element_new], placeinlist, placeinlist);
}

makeMenu()
{
	gMenu=[];
	gMenu+=sideMenu;
	if(llList2Integer(statesMenu,0)==1)gMenu+="[x]Chat";
	else gMenu+="[ ]Chat";
	if(llList2Integer(statesMenu,1)==1)gMenu+="[x]Emote";
	else gMenu+="[ ]Emote";
	if(llList2Integer(statesMenu,2)==1)gMenu+="[x]Read IM";
	else gMenu+="[ ]Read IM";
	if(llList2Integer(statesMenu,3)==1)gMenu+="[x]Send IM";
	else gMenu+="[ ]Send IM";
	gMenu+=["Name","Add Chat","Add Emote","Reset"];

	//Than display
	channel_dialog = ( -1 * (integer)("0x"+llGetSubString((string)llGetKey(),-5,-1)) );
	listen_id = llListen( channel_dialog, "", NULL_KEY, "");
	llDialog(ToucherID, msg,gMenu, channel_dialog);
	llSetTimerEvent(45.0);
}

default {
    state_entry() 
    {
    	 wearer=llKey2Name(llGetOwner());
    	objektname=llGetObjectName(); 
    	if(gChat==[])
		{
			glCardRows=[];
			gsCardName="Chat";
			giLine=0;
			gkQuery = llGetNotecardLine(gsCardName,giLine);
		}
    }
    
    dataserver(key _query_id, string _data) 
    {
        if (_query_id == gkQuery) 
        {
            if (_data == EOF) 
            {
            	if(gsCardName=="Chat")
            	{
            		
            		gChat=glCardRows;
					glCardRows=[];
					gsCardName="Emote";
					giLine=0;
					gkQuery = llGetNotecardLine(gsCardName,giLine);
						
            	}
            	else if(gsCardName=="Emote")
            	{
            		
            		gEmote=glCardRows;
            		glCardRows=[];
            	}
            }
            else 
            {
               	glCardRows=glCardRows+_data;
                ++giLine;
                gkQuery = llGetNotecardLine(gsCardName, giLine);
            }
        }
    }
    
    listen(integer channel, string name, key id, string message)
    {
    	if(channel==channel_dialog)
    	{
			llListenRemove(listen_id);
			llSetTimerEvent(0.0);
	    	if(message=="[x]Chat")
	    	{
	    		llLRP(0,0);
	    		llRlvSay("redirchat:"+(string)chatChan+"=rem");
	    		llListenRemove(listen_id2);
	    		makeMenu();
	    	}
			if(message=="[ ]Chat")
			{
				llLRP(1,0);
				llRlvSay("redirchat:"+(string)chatChan+"=add");
				listen_id2 = llListen( chatChan, "", llGetOwner(), "");
				makeMenu();
			}
			if(message=="[x]Emote")
			{
				llLRP(0,1);
				llRlvSay("rediremote:"+(string)emoteChan+"=rem");
				llListenRemove(listen_id3);
				makeMenu();
			}
			if(message=="[ ]Emote")
			{
				llLRP(1,1);
				llRlvSay("rediremote:"+(string)emoteChan+"=add");
				listen_id3= llListen( emoteChan, "", llGetOwner(), "");
				makeMenu();
			}
			if(message=="[x]Read IM")
			{
				llLRP(0,2);
				llRlvSay("recvim_sec=y");
				makeMenu();
			}
			if(message=="[ ]Read IM")
			{
				llLRP(1,2);
				llRlvSay("recvim_sec=n");
				makeMenu();
			}
			if(message=="[x]Send IM")
			{
				llLRP(0,3);
				llRlvSay("sendim_sec=y");
				makeMenu();
			}
			if(message=="[ ]Send IM")
			{
				llLRP(1,3);
				llRlvSay("sendim_sec=n");
				makeMenu();
			}
			if(message=="Name")
			{
                listen_id4 = llListen( rand, "", ToucherID, "");
                llInstantMessage(ToucherID, "Change name: /"+(string)rand +" <Name>");
			}
			if(message=="Add Chat")
			{
				listen_id5 = llListen( addChat, "", ToucherID, "");
                llInstantMessage(ToucherID, "Add Chat: /"+(string)addChat +" <Text>");
			}
			if(message=="Add Emote")
			{
				listen_id6 = llListen( addEmote, "", ToucherID, "");
                llInstantMessage(ToucherID, "Add Emote: /"+(string)addEmote +" <Text>");
			}
			if(message=="Reset")
			{
				gChat =[];
				gEmote=[];
				if(gChat==[])
				{
					glCardRows=[];
					gsCardName="Chat";
					giLine=0;
					gkQuery = llGetNotecardLine(gsCardName,giLine);
				}
			}
			if(message=="Back")llMessageLinked(LINK_THIS, gMenuLink, "DV_MAIN_MISC", ToucherID);
    	}
    	if(channel==chatChan)
    	{
    		//say something out of chat list
    		float grand=llFrand(llGetListLength(gChat)-1);
            integer random= llRound(grand);
            llSetObjectName(wearer);
            string saying =llList2String(gChat,random);
            llSay(0, saying);
            llSetObjectName(objektname);
    	}
    	if(channel==emoteChan)
    	{
    		//say something out of emote list
    		float grand=llFrand(llGetListLength(gEmote)-1);
            integer random= llRound(grand);
            llSetObjectName(wearer);
            string saying =llList2String(gEmote,random);
            llSay(0, "/me "+saying);
            llSetObjectName(objektname);
    	}
    	if(channel==rand)
    	{
    		llListenRemove(listen_id4);
    		wearer=message;
    	}
    	if(channel==addChat)
    	{
    		llListenRemove(listen_id5);
    		gChat+=message;
    	}
    	if(channel==addEmote)
    	{
    		llListenRemove(listen_id6);
    		gEmote+=message;
    	}
		
    }
    
    link_message(integer sender, integer num, string str, key id)
  	{
  		
  		if(num ==gMenuLink)
  		{
  			if(str==llGetScriptName())
  			{
  				llMessageLinked(LINK_THIS, gDemon, llGetScriptName(), NULL_KEY);
  				ToucherID=id;
  				makeMenu();
  			}
  		}
  		if(num ==gDemon)
  		{
  			if(str =="attachment")attachment=TRUE;
  			else if(str =="device")attachment=FALSE;
  			else if(str!=llGetScriptName())
  			{
				llListenRemove(listen_id);
				llListenRemove(listen_id4);
				llListenRemove(listen_id5);
				llListenRemove(listen_id6);

  			}
  		}
  		if(num==gPlugin)
  		{
  			if(str=="ON_REZ")
  			{
  				MakeRLVState();
  			}
  			if(str=="RESET")
  			{
  				statesMenu=[0,0,0,0];
  			}
  		}
  		if(num==gVic)
  		{
  				victim=(key)str;
  				cmdname = (string)llGetKey();
  		}
  	}
  	
  	timer()
    {
    	llSetTimerEvent(0.0);
    	llListenRemove(listen_id);
    }
}
